package com.megvii.koala.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.megvii.corelib.components.BaseNetworkErrorView;
import com.megvii.koala.R;
import com.megvii.koala.activity.params.SimpleWebViewParam;
import com.megvii.koala.base.KoalaActivity;
import com.megvii.koala.components.TitleView;
import com.megvii.koala.presenter.SimpleWebViewPresenter;

/**
 * Created by linyue on 16/3/2.
 */
public class SimpleWebViewActivity extends KoalaActivity<SimpleWebViewParam>
        implements TitleView.TitleListener
{
    protected TitleView titleView;

    protected WebView webView;

    private SimpleWebViewListener listener;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_webview_layout);
        titleView = (TitleView) findViewById(R.id.simple_webview_layout_title);
        titleView.setTitleListener(this);
        webView = (WebView) findViewById(R.id.simple_webview_layout_webview);
        webView.setBackgroundColor(0x00000000);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.setWebViewClient(new EmptyViewClientClient());
        webView.setWebChromeClient(new EmptyChromeChromeClient());
    }

    public void setTitleText(String text)
    {
        titleView.setTitleText(text);
    }

    public void loadUrl(String url)
    {
        webView.loadUrl(url);
    }

    @Override
    public void onBackPressed()
    {
        if (webView.canGoBack())
        {
            webView.goBack();
        }
        else
        {
            super.onBackPressed();
        }
    }

    @Override
    public void onClickReturn()
    {

    }

    @Override
    public void onClickFunction()
    {
        if (listener != null)
        {
            listener.onClickClose();
        }
    }

    @Override
    public Class getPresenterClass()
    {
        return SimpleWebViewPresenter.class;
    }

    @Override
    public BaseNetworkErrorView getNetworkErrorView()
    {
        return null;
    }

    public void setListener(SimpleWebViewListener listener)
    {
        this.listener = listener;
    }

    public interface SimpleWebViewListener
    {
        public void onClickClose();
    }

    class EmptyViewClientClient extends WebViewClient
    {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description,
                String failingUrl)
        {
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon)
        {
        }

        @Override
        public void onPageFinished(WebView view, String url)
        {
        }
    }

    class EmptyChromeChromeClient extends WebChromeClient
    {
        public void onProgressChanged(WebView view, int newProgress)
        {
        }
    }
}
